import { createStore } from "vuex";

const KEY = "CARTLIST";

const setCarlistData = (state) => {
  localStorage.setItem(KEY, JSON.stringify(state.cartList));
};

const getCartlistData = () => {
  try {
    return JSON.parse(localStorage.getItem(KEY));
  } catch (error) {
    return {};
  }
};

export default createStore({
  state: {
    iconPicList: [
      {
        url: require("@/assets/navicons/chaoshi.png"),
        txt: "超市",
      },
      {
        url: require("@/assets/navicons/caishichang.png"),
        txt: "菜市场",
      },
      {
        url: require("@/assets/navicons/daipaimianyun.png"),
        txt: "大牌免运",
      },
      {
        url: require("@/assets/navicons/dangao.png"),
        txt: "烘焙蛋糕",
      },
      {
        url: require("@/assets/navicons/hongbao.png"),
        txt: "红包套餐",
      },
      {
        url: require("@/assets/navicons/jiaju.png"),
        txt: "家居时尚",
      },
      {
        url: require("@/assets/navicons/qiandao.png"),
        txt: "签到",
      },
      {
        url: require("@/assets/navicons/shuiguodian.png"),
        txt: "水果店",
      },
      {
        url: require("@/assets/navicons/xianhua.png"),
        txt: "鲜花绿植",
      },
      {
        url: require("@/assets/navicons/yiyaojiankang.png"),
        txt: "医药健康",
      },
    ],
    //  shopId:{
    //   shopName:"",
    //   productList:{
    //     id:{}
    //   }
    //  }
    cartList: getCartlistData(),
  },
  getters: {},
  mutations: {
    addToCartStore(state, { shopId, productId, info, shopName }) {
      let shopInfo = state.cartList[shopId] || {
        shopName: shopName,
        productList: {},
      };
      let product = shopInfo.productList[productId];
      if (!product) {
        product = info;
        product.count = 0;
      }
      product.count += 1;
      if (product.count > 0) {
        product.check = true;
      }
      shopInfo.productList[productId] = product;
      state.cartList[shopId] = shopInfo;
      setCarlistData(state);
    },
    minusToCartStore(state, { shopId, productId, info, shopName }) {
      let shopInfo = state.cartList[shopId] || {
        shopName: shopName,
        productList: {},
      };

      let product = shopInfo.productList[productId];

      if (!product) {
        product = info;
        product.count = 0;
      }
      product.count -= 1;
      if (product.count < 0) {
        product.count = 0;
        return;
      }
      shopInfo.productList[productId] = product;
      state.cartList[shopId] = shopInfo;
      setCarlistData(state);
    },
    handleCheckStore(state, { shopId, id }) {
      state.cartList[shopId].productList[id].check =
        !state.cartList[shopId].productList[id].check;
      setCarlistData(state);
    },
    //清空购物车
    clearCartStore(state, shopId) {
      for (let k in state.cartList[shopId].productList) {
        let productListItem = state.cartList[shopId].productList[k];
        productListItem.count = 0;
        productListItem.check = false;
      }
      setCarlistData(state);
    },
    //全选
    selectAllStore(state, shopId) {
      let shopInfo = state.cartList[shopId].productList;
      for (let key in shopInfo) {
        if (shopInfo[key].count > 0 && shopInfo[key].check === false) {
          shopInfo[key].check = true;
        }
      }
      setCarlistData(state);
    },
    //清理购物车
    clearCartList(state, shopId) {
      state.cartList[shopId].productList = {};
      setCarlistData(state);
    },
  },
  actions: {},
  modules: {},
});
